package com.ayf.payment.game.api.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ayf.payment.game.api.entity.platform.SmsChannelConfig;
import com.ayf.payment.game.api.entity.platform.SmsContent;
import com.ayf.payment.game.api.entity.platform.SmsRecord;
import com.ayf.payment.game.api.httpclient.HttpUtils;
import com.ayf.payment.game.api.service.platform.SmsContentService;
import com.ayf.payment.game.api.type.SMSType;
import com.ayf.payment.game.api.utils.MD5Util;
import com.ayf.payment.game.api.utils.RandomStrUtils;
import com.ayf.payment.game.api.utils.SnowFlakeUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 35mt短信
 */
@Component
public class SwMtSMSHandler extends SMSHandler {

    @Autowired
    SmsContentService smsContentService;


    public void send(String phone,SMSType type, SmsChannelConfig sMSConfig, Integer merchantId) {
        JSONObject config = JSONObject.parseObject(sMSConfig.getChannelConfig());
        final String url = config.getString("url");
        final String account = config.getString("account");
        final String password = config.getString("password");
        final String smsSign = config.getString("sign");
        SmsContent smsContent = smsContentService.getSmsContentBySmsChannelCodeAndSmsType(sMSConfig.getChannelCode(),type.name());
        String smsMsg = "尊敬的用户，您的验证码{0}，有效期为5分钟。";
        if (smsContent != null){
            smsMsg = smsContent.getSmsContent();
        }
        String code = RandomStrUtils.createRandom(true, 6);
        String msg = smsSign.concat(MessageFormat.format(smsMsg,code));
        String tKey = String.valueOf(System.currentTimeMillis()/1000);
        Map<String, String> data = new HashMap<>();
        data.put("username",account);
        data.put("password", MD5Util.strToMd5Utf8(MD5Util.strToMd5Utf8(password).concat(tKey)));
        data.put("tKey",tKey);
        data.put("mobile",phone);
        data.put("content",msg);
        data.put("time","");
        data.put("ext","111111");
        data.put("extend","222222");

        String traceId = SnowFlakeUtils.getTraceId();
        try {
            Map<String, String> map = HttpUtils.noSignPost(traceId, url, JSON.toJSONString(data));
            SmsRecord smsRecord = new SmsRecord();
            String channelRpsCode = map.get(HttpUtils.RESPONSE_CODE);
            if ("200".equals(channelRpsCode)) {
                String dataResult = map.get(HttpUtils.RESPONSE_DATA);
                JSONObject jsResult = JSONObject.parseObject(dataResult);
                String resCode = jsResult.getString("code");
                if ("200".equals(resCode)) {
                    smsRecord.setStatus(1);
                } else {
                    smsRecord.setStatus(0);
                    String errorMsg = "其他错误";
                    switch (resCode){
                        case "4001":
                            errorMsg = "用户名错误";
                            break;
                        case "4002":
                            errorMsg = "密码不能为空";
                            break;
                        case "4004":
                            errorMsg = "手机号码错误";
                            break;
                        case "4006":
                            errorMsg = "IP 鉴权错误";
                            break;
                        case "4007":
                            errorMsg = "用户禁用";
                            break;
                        case "4008":
                            errorMsg = "tKey 错误";
                            break;
                        case "4009":
                            errorMsg = "密码错误";
                            break;
                        case "4013":
                            errorMsg = "定时时间错误";
                            break;
                        case "4014":
                            errorMsg = "模板错误";
                            break;
                        case "4015":
                            errorMsg = "扩展号错误";
                            break;
                        case "4019":
                            errorMsg = "用户类型错误";
                            break;
                        case "4023":
                            errorMsg = "签名错误";
                            break;
                        case "4025":
                            errorMsg = "模板变量内容为空";
                            break;
                        case "4026":
                            errorMsg = "手机号码数最大 2000 个";
                            break;
                        case "4027":
                            errorMsg = "模板变量内容最大 200 组";
                            break;
                        case "4029":
                            errorMsg = "请使用 post 请求";
                            break;
                        case "4030":
                            errorMsg = "Content-Type 请使用application/json";
                            break;
                        case "9998":
                            errorMsg = "JSON 解析错误";
                            break;
                        case "9999":
                            errorMsg = "非法请求";
                            break;
                    }
                    LogPortal.error("{} 35平台短信验证码发送失败,发送手机号:[{}]发送内容:[{}] 错误码:{} 错误描述:{}",traceId,phone,msg,resCode,errorMsg);
                }
                smsRecord.setChannelMsgid(jsResult.getString("msgId"));
                smsRecord.setResponseCode(resCode);
                smsRecord.setResponseCodeMsg(jsResult.getString("msg"));
                smsRecord.setSendPhone(phone);
                smsRecord.setSmsChannel(sMSConfig.getChannelName());
                smsRecord.setSendContent(msg);
            } else {
                smsRecord.setStatus(0);
                smsRecord.setResponseCodeMsg("35平台短信接口调用失败，http状态码:"+channelRpsCode+"，错误码:".concat(traceId));
            }
            save(smsRecord, type, code, merchantId);
        } catch (IOException e) {
            LogPortal.error("[{}]35平台短信验证码发送失败 发送{}异常，发送手机号:[{}]发送内容为:[{}]",e,traceId,e.getClass().getName(),phone,msg);
        }
    }
}
